package lc.q51_100;


import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class Q98 {

    public boolean isValidBST(TreeNode root) {
        // 一边遍历一边寻找
        if (root == null) {
            return true;
        }
        Deque<TreeNode> stack = new ArrayDeque<>();
        Integer prev = null;
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.addFirst(root);
                root = root.left;
            }
            TreeNode node = stack.removeFirst();
            if (prev == null || prev < node.val) {
                prev = node.val;
            } else {
                return false;
            }
            // 遍历右子树
            root = node.right;
        }
        return true;
    }

}
